import baseservice from './baseservice.js'
export default {
	getPageData(self, successBack, errorBack) {
		baseservice.getPageData(self, self.apis.getDicData, self.searchcondition, function(res) {
			self.tableData = self.formattool.trimNull(res.data.list, []);
			for(let i in self.tableData) {
				let content = self.tableData[i].dictValue.replace(/<[^>]+>|&nbsp;/g, "");
				if(content.length > 100) {
					self.tableData[i].dictValue = content.substring(0, 100) + "...";
				}
				console.log(self.tableData[i].dictValue);
			}
			self.total = res.data.total;
		});
	},
	getModelData(self, id, successBack, errorBack) {
		baseservice.getModelData(self, self.apis.getDicDetail, id);
	},
	showDetail(self, row, title) {
		baseservice.showDetail(self, row, title);
	},
	handleSave(self, successBack, errorBack) {
		let that = this;
		if(self.isAddable == false) {
			self.modelData.dictValue = self.$refs.ue.getUEContent().replace("<p><br></p>", "");
		}
		let rule = [{
			name: 'dictKey',
			label: '编码',
			rule: ['validateEmpty_']
		}, {
			name: 'dictValue',
			label: '内容',
			rule: ['validateEmpty_']
		}, {
			name: 'seqNo',
			label: '排序编号',
			rule: ['validateNum_']
		}]
	
		if(!self.validatetool.validate(self, self.modelData, rule)) {
			return;
		}
		let msg = '添加成功';
		let requestUrl = 'dict/save';
		if(self.id) {
			requestUrl = 'dict/update'
			msg = '修改成功';
		};
		
		self.httptool.ajaxPost(requestUrl, self.modelData, function(res) {
			self.$message({
				message: msg,
				type: 'success'
			});
			self.close();
			self.$emit('close', true);
			self.$store.commit('clearstore');
		});

	},
	handleDelete(self, id, successBack, errorBack) {
		let that = this;
		let param = {};
		param.id = id;
		self.$confirm(self.msg.delNotice, '提示', {
			confirmButtonText: '确定',
			cancelButtonText: '取消',
			type: 'warning'
		}).then(() => {
			self.httptool.ajaxPost(self.apis.delDic, param, function(res) {
				if(res.code == 1) {
					that.getPageData(self);
					self.$message('删除成功！');
				} else {
					self.$message('删除失败！');
				}
				self.$store.commit('clearstore');
			});
		}).catch(() => {

		});
	},
	getDicTypes(self, parentId, successBack, errorBack) {
		let pageTypes = {
			pageNum: 1,
			pageSize: 1000,
			parentId: parentId
		};
		self.httptool.ajaxPost(self.apis.getDicTypes, pageTypes, function(res) {
			if(res.code == 1) {
				if(successBack) {
					successBack(res);
				}
			}
		});
	}
}